/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.data;

import javautil.collections.ArrayUtil;

import java.util.Iterator;
import java.util.LinkedHashSet;

/**
 * Physical Data Source : this generalizes P_FileDataSource and P_DBDataSource
 * @author Susanta Tewari
 * @version 1.0
 * @updated 09-Nov-2007 10:19:17 AM
 */
public abstract class PDataSource extends DataSource {

    /**
     * physical length of the chromosome indicated by ch_id
     */
    private int chLength = 0;

    /**
     * number of clones
     */
    private int cloneCount = 0;

    /**
     * physical length of a single clone
     */
    private int cloneLength = 0;

    /**
     * Hybridization Data
     */
    private int[][] hybridData = null;

    /**
     * number of probes
     */
    private int probeCount = 0;

    /**
     * Probability of false positive
     */
    private double probFalsePos = 0.00002;

    /**
     * probability of false negative
     */
    private double probFalseNeg = 0.00002;

    /** Field description */
    private ChData chData = null;

    public abstract LinkedHashSet<Clone> getCloneSet();

    public LinkedHashSet<Probe> getProbeSet() {

        LinkedHashSet<Probe> fullProbeSet = getFullProbeSet();
        LinkedHashSet<Probe> reqSet       = new LinkedHashSet<>();
        Iterator<Probe> itrProbe          = fullProbeSet.iterator();

        while (itrProbe.hasNext()) {

            Probe probe = itrProbe.next();

            if (ArrayUtil.contains(getOrder(), null, probe.getOrder())) reqSet.add(probe);
        }

        return reqSet;
    }

    public abstract LinkedHashSet<Probe> getFullProbeSet();

    /**
     * physical length of the chromosome indicated by ch_id
     */
    public int getChLength() {
        return chLength;
    }

    /**
     * number of clones
     */
    public int getCloneCount() {
        return cloneCount;
    }

    /**
     * physical length of a single clone
     */
    public int getCloneLength() {
        return cloneLength;
    }

    /**
     * Hybridization Data
     */
    public int[][] getHybridData() {

        if (hybridData == null) loadData();

        return hybridData;
    }

    /**
     * number of probes
     */
    public int getProbeCount() {
        return probeCount;
    }

    /**
     * Probability of false positive
     */
    public double getProbFalsePos() {
        return probFalsePos;
    }

    /**
     * probability of false negative
     */
    public double getProbFalseNeg() {
        return probFalseNeg;
    }

    /**
     * physical length of the chromosome indicated by ch_id
     *
     * @param newVal
     */
    public void setChLength(int newVal) {
        chLength = newVal;
    }

    /**
     * number of clones
     *
     * @param newVal
     */
    public void setCloneCount(int newVal) {
        cloneCount = newVal;
    }

    /**
     * physical length of a single clone
     *
     * @param newVal
     */
    public void setCloneLength(int newVal) {
        cloneLength = newVal;
    }

    /**
     * number of probes
     *
     * @param newVal
     */
    public void setProbeCount(int newVal) {
        probeCount = newVal;
    }

    /**
     * Probability of false positive
     *
     * @param newVal
     */
    public void setProbFalsePos(double newVal) {
        probFalsePos = newVal;
    }

    /**
     * probability of false negative
     *
     * @param newVal
     */
    public void setProbFalseNeg(double newVal) {
        probFalseNeg = newVal;
    }

    /**
     * Hybridization Data
     *
     * @param newVal    newVal
     */
    public void setHybridData(int[][] newVal) {
        this.hybridData = newVal;
    }

    /**
     * for Genetic Map:RFLP data and Physical Map HYBRIDIZATION data
     */
    public int[][] getdata() {
        return reOrderData(getHybridData(), getOrder());
    }

    /**
     * Method description
     *
     * @param data description
     * @param order description
     *
     * @return description
     */
    private static int[][] reOrderData(int[][] data, int[] order) {

        int[][] newData = new int[data.length][order.length];

        for (int colIndex = 0; colIndex < order.length; colIndex++) {

            for (int rowIndex = 0; rowIndex < data.length; rowIndex++) {

                newData[rowIndex][colIndex] = data[rowIndex][order[colIndex] - 1];
            }
        }

        return newData;
    }

    public ChData getChData() {
        return chData;
    }

    public void setChData(ChData chData) {
        this.chData = chData;
    }
}
